package cn.changhong.user.controller;


import cn.changhong.user.aop.ResultBean;
import cn.changhong.user.model.vo.UserVO;
import cn.changhong.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author zz
 * @Date
 **/
@RestController
@RequestMapping("user_manage/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "register",method = RequestMethod.POST)
    @CrossOrigin
    public ResultBean register(@RequestParam("email")String email,
                               @RequestParam("password")String password){
        return userService.register(email,password);
    }

    @RequestMapping(value = "admin/listUserByPage",method = RequestMethod.GET)
    @CrossOrigin
    public ResultBean listUserByPage(@RequestParam("currentPage") int currentPage,
                                                 @RequestParam("pageSize")int pageSize){
        return userService.listUserByPage(currentPage,pageSize);
    }

    @RequestMapping(value = "admin/removeUser",method = RequestMethod.DELETE)
    @CrossOrigin
    public ResultBean<Integer> removeUser(@RequestParam("email") String email){
        return new ResultBean( userService.removeUser(email));
    }

    @RequestMapping(value = "admin/freezeUser",method = RequestMethod.PUT)
    @CrossOrigin
    public ResultBean<Integer> freezeUser(@RequestParam("email") String email){
        return new ResultBean(userService.freezeUser(email));
    }

    @RequestMapping(value = "commonUser/resetPassword",method = RequestMethod.PUT)
    @CrossOrigin
    public ResultBean<Integer> resetPassword(@RequestParam("userId") Long userId,
                                             @RequestParam("password") String password){
        return new ResultBean( userService.resetPassword(userId,password));
    }

    @RequestMapping(value = "commonUser/forgetPassword",method = RequestMethod.GET)
    @CrossOrigin
    public ResultBean<Integer> forgetPassword(@RequestParam("email") String email){
        return new ResultBean( userService.forgetPassword(email));
    }



    @RequestMapping(value = "superAdmin/addUser",method = RequestMethod.POST)
    @CrossOrigin
    public ResultBean<Integer> addUser(@RequestParam("email") String email,
                                      @RequestParam("roleId")int roleId){
        return new ResultBean(userService.addUser(email,roleId));
    }

    @RequestMapping(value = "commonUser/getUser",method = RequestMethod.GET)
    @CrossOrigin
    public ResultBean<UserVO> getUser(HttpServletRequest request){
        return new ResultBean(userService.getUser(request));
    }

    @RequestMapping(value = "commonUser/updatePassword",method = RequestMethod.PUT)
    @CrossOrigin
    public ResultBean updatePassword(HttpServletRequest request,
                                              @RequestParam("oldPassword") String oldPassword,
                                              @RequestParam("newPassword")String newPassword,
                                              @RequestParam("confirmPassword")String confirmPassword){
        return userService.updatePassword(request,oldPassword,newPassword,confirmPassword);
    }
}
